package utils.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.lanou.horo.myapplication.R;

import java.util.List;

import entity.SearchEntity;
import volley.VolleySingleton;

/**
 * 搜索
 * Created by dllo on 15/11/30.
 */
public class SearchListAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private Context context;
    private SearchEntity searchEntity;

    public void cleanList(){
        searchEntity = null;
        notifyDataSetChanged();
    }
    public SearchListAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    public void addData(SearchEntity searchEntity){
        this.searchEntity = searchEntity;
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        if (searchEntity != null) {
            if (isAlbumListNull()) {
                if (isResultListNull()) {
                    return searchEntity.getAlbumResultList().size() + searchEntity.getQueryResultList().size();
                }
                return searchEntity.getAlbumResultList().size();
            }
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    private boolean isResultListNull() {
        return searchEntity.getQueryResultList() != null && searchEntity.getQueryResultList().size() > 0;
    }

    private boolean isAlbumListNull() {
        return searchEntity.getAlbumResultList() != null && searchEntity.getAlbumResultList().size() > 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SearchViewHolder sviewHolder = new SearchViewHolder();
            convertView = inflater.inflate(R.layout.search_listview_query, parent, false);
            sviewHolder.searchQianIm = (NetworkImageView) convertView.findViewById(R.id.searchQianIm);
            sviewHolder.listview_query_title = (TextView) convertView.findViewById(R.id.listview_query_title);
            sviewHolder.searchHouTv = (TextView) convertView.findViewById(R.id.searchHouTv);
        if (searchEntity != null ) {
            if (isAlbumListNull()) {
                if (position < searchEntity.getAlbumResultList().size()) {
                    sviewHolder.listview_query_title.setText(searchEntity.getAlbumResultList().get(position).getKeyword());
                    sviewHolder.searchHouTv.setText(searchEntity.getAlbumResultList().get(position).getCategory());
                    VolleySingleton.getInstance().getBitmap(searchEntity.getAlbumResultList().get(position).
                            getImgPath(), sviewHolder.searchQianIm);
                }else {
                    if (isResultListNull()) {
                        sviewHolder.listview_query_title.setText(searchEntity.getQueryResultList().get(position-searchEntity.getAlbumResultList().size()).getKeyword());
                    }
                }
            }

        }
        return convertView;
    }

    private class SearchViewHolder {
        NetworkImageView searchQianIm;
        TextView listview_query_title, searchHouTv;
    }
}
